package com.hkedou.yuanhuo.token;

import com.hkedou.yuanhuo.shiro.redis.IRedisManager;
import com.hkedou.yuanhuo.shiro.redis.RedisConf;
import com.hkedou.yuanhuo.shiro.redis.RedisManager;
import com.hkedou.yuanhuo.shiro.redis.RedisTokenManager;
import com.hkedou.yuanhuo.shiro.redis.impl.RedisTokenManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;

@Configuration
public class TokenInterceptorConfig extends WebMvcConfigurerAdapter {

    @Resource
    private RedisConf redisConf;

    /**
     * 配置shiro redisManager
     * 使用的是shiro-redis开源插件
     * @return
     */
    public IRedisManager redisManager() {
        RedisManager redisManager = new RedisManager();
        redisManager.setHost(redisConf.getHost());
        redisManager.setPort(redisConf.getPort());
//        redisManager.setExpire(1800);// 配置缓存过期时间
        redisManager.setTimeout(redisConf.getTimeout());
        redisManager.setPassword(redisConf.getPassword());
        redisManager.setDatabase(redisConf.getDatabase());
        return redisManager;
    }


    @Bean
    public RedisTokenManager redisTokenManager(){
        RedisTokenManagerImpl redisTokenManager = new RedisTokenManagerImpl();
        redisTokenManager.setRedisManager(redisManager());
        return redisTokenManager;
    }

    @Bean
    public TokenInterceptor getTokenInterceptor(){
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTokenInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}
